//
//  ProductInfoViewController.m
//  MJApp
//
//  Created by wangliang on 15/3/16.
//  Copyright (c) 2015年 wangliang. All rights reserved.
//

#import "ProductInfoViewController.h"
#import "ProductDownView.h"
#import "ShareView.h"
#import "ProductDetailTvc.h"
#import "confrimOrderVc.h"
#import "ProductDetailTvc.h"
#import "ProductInfoViewControllerCell.h"
#import "ShareModel.h"
#import "ConfrimOrderViewController.h"


@interface ProductInfoViewController ()<ProductDetailTvcProtocol,UITableViewDataSource,UITableViewDelegate,ProductDownViewDelegate,UMSocialUIDelegate>
@property (weak, nonatomic) IBOutlet UIView *downBackView;
@property (weak, nonatomic) IBOutlet UICollectionView *collectionView;
@property (weak, nonatomic) IBOutlet UIView *pageBackView;
@property (weak, nonatomic) IBOutlet UITableView *tableView;

@property (weak, nonatomic) IBOutlet NSLayoutConstraint *between;
@property (weak, nonatomic) IBOutlet UIButton *detailBtn;
@property (weak, nonatomic) IBOutlet UIButton *shareBtn;
@property (weak, nonatomic) IBOutlet UIButton *addShopCarBtn;
@property (weak, nonatomic) IBOutlet UILabel *nowPriceLabel;
@property (weak, nonatomic) IBOutlet UILabel *oldPriceLabel;
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *xPostion;
@property (nonatomic,weak) UIPageControl * pageControl;
@property (nonatomic, strong)ProductDownView * downView;

//@property (nonatomic, )

@end

@implementation ProductInfoViewController

- (void)viewDidLoad {
    [super viewDidLoad];
//    self.navigationController.title = @"商品详情";
//    [self NaviUI];
    [self UI];
    self.automaticallyAdjustsScrollViewInsets = NO;
    self.navigationController.navigationBar.translucent = NO;
    self.navigationItem.title = self.detailDic[@"name"];
    
    
    CGFloat priceHigh = [self.detailDic[@"price_high"] floatValue];
    CGFloat priceLow = [self.detailDic[@"price_low"] floatValue];
    self.oldPriceLabel.text = [NSString stringWithFormat:@"¥%0.2f",priceHigh];
    self.nowPriceLabel.text = [NSString stringWithFormat:@"¥%0.2f",priceLow];
}


- (void)NaviUI {
    
//    UIButton *btn = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 33, 19)];
////    [btn setBackgroundImage:LOAD_IMAGE(@"搜索@2x.png") forState:UIControlStateNormal];
//    [btn addTarget:self action:@selector(btnAction:) forControlEvents:UIControlEventTouchUpInside];
//    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:btn] ;
    
    NSDictionary *textDic = [NSDictionary dictionaryWithObjectsAndKeys:[UIColor whiteColor],NSForegroundColorAttributeName,[UIFont boldSystemFontOfSize:20],NSFontAttributeName ,nil];
    [self.navigationController.navigationBar setTitleTextAttributes:textDic];
    
    [self.navigationController.navigationBar setBackgroundImage:[UIImage createImageWithColor:Color_Back_Black] forBarMetrics:UIBarMetricsDefault];
    self.navigationController.navigationBar.translucent = NO;//去掉透明度
    
    UIButton *rightbtn = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 18, 20)];
    [rightbtn setBackgroundImage:LOAD_IMAGE(@"sc1_03.png") forState:UIControlStateNormal];
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:rightbtn];
    [rightbtn setTitleColor:RGB(100, 95, 136) forState:UIControlStateNormal];
    [rightbtn setTitle:@"0" forState:UIControlStateNormal];
    [rightbtn setContentEdgeInsets:UIEdgeInsetsMake(4, 2, 0, 0)];
    rightbtn.titleLabel.font = [UIFont systemFontOfSize:12];
    
}

- (void)btnAction:(UIButton *)sender {

    [self.navigationController popViewControllerAnimated:YES];
    [self.navigationItem setBackBarButtonItem:nil];
}

- (void)UI {
    
    [((TabBarController *)self.tabBarController) setTabbarHidden:YES];
    ProductDownView *downView = [[ProductDownView alloc] initWithFrame:self.downBackView.bounds];
    downView.delegate = self;
    downView.imagesArr = self.detailDic[@"images"];
    [self.downBackView addSubview:downView];
    self.downView = downView;
    
    self.pageBackView.layer.anchorPoint = CGPointMake(0.5, 0.5);
    self.pageBackView.transform = CGAffineTransformMakeRotation(M_PI / 2);
    self.between.constant = CGRectGetHeight(self.pageBackView.frame) / 2 - CGRectGetWidth(self.pageBackView.frame) / 2;
    
    UIPageControl *pageControl = [[UIPageControl alloc] initWithFrame:CGRectMake(0, 0, CGRectGetHeight(self.pageBackView.frame), CGRectGetWidth(self.pageBackView.frame))];
    pageControl.numberOfPages = ((NSArray *)self.detailDic[@"images"]).count;
    pageControl.pageIndicatorTintColor = [UIColor grayColor];

    pageControl.currentPageIndicatorTintColor = [UIColor whiteColor];
    pageControl.currentPage = 0;
    self.pageControl = pageControl;
    CGSize pageControlSize = pageControl.size;
    [self.pageBackView addSubview:pageControl];
    self.xPostion.constant = -pageControl.width/2.0+pageControlSize.height/2.0;
    self.tableView.pagingEnabled =YES;
    
    self.shareBtn.layer.borderWidth = 0.5;
    self.shareBtn.layer.borderColor = [UIColor blackColor].CGColor;
    self.detailBtn.layer.borderWidth = 0.5;
    self.detailBtn.layer.borderColor = [UIColor blackColor].CGColor;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (IBAction)detailBtnClick:(UIButton *)sender {
//    ProductDetailInfoCell
    ProductDetailTvc * tvc = [[NSBundle mainBundle] loadNibNamed:@"ProductDetailTvc" owner:nil options:nil].lastObject;
    tvc.delegate = self;
    tvc.detailDic = self.detailDic;
//    tvc.y = 50; 
    [UIView animateWithDuration:0.2 animations:^{
        tvc.y = 0;
    }];
    [self.view addSubview:tvc];
//[[self.navigationController pushViewController:[[ProductDetailTvc alloc] init] animated:YES];
    
    
}
- (IBAction)shareBtnClick:(UIButton *)sender {
    
    NSString *shareText = self.detailDic[@"name"];             //分享内嵌文字
    
    NSMutableArray *arrayImages = [NSMutableArray array];
    for (NSDictionary *dic in self.detailDic[@"images"]) {
        
        UIImageView *imageView = [[UIImageView alloc] init];
        NSURL *url = [NSURL URLWithString:[NSString stringWithFormat:@"%@%@",kUrl,dic[@"image"]]];
        [imageView sd_setImageWithURL:url completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType, NSURL *imageURL) {
            
            [arrayImages addObject:image];
        }];
        
    }
//    //调用快速分享接口
//    NSMutableArray *snsNames = [NSMutableArray array];
//    [snsNames addObject:UMShareToWechatSession];
//    [snsNames addObject:UMShareToWechatTimeline];
//    [snsNames addObject:UMShareToQQ];
//    [snsNames addObject:UMShareToQzone];
//    [snsNames addObject:UMShareToSina];
//    [UMSocialSnsService presentSnsIconSheetView:self
//                                         appKey:UM_AppKey
//                                      shareText:shareText
//                                     shareImage:arrayImages[0]
//                                shareToSnsNames:snsNames
//                                       delegate:self];
    NSMutableDictionary *dic = [NSMutableDictionary dictionary];
    dic[@"text"] = shareText;
    dic[@"image"] = arrayImages;
    [ShareModel shareModelWithObject:self withDic:dic];
    
    

}
- (IBAction)addShopCarBtnClick:(UIButton *)sender {
    
    ProductDetailTvc * tvc = [[NSBundle mainBundle] loadNibNamed:@"ProductDetailTvc" owner:nil options:nil].lastObject;
    tvc.delegate = self;
    tvc.detailDic = self.detailDic;
    //    tvc.y = 50;
    [UIView animateWithDuration:0.2 animations:^{
        tvc.y = 0;
    }];
    [self.view addSubview:tvc];
}

-(void)ProductDetailTvcToConfirmOrder:(ProductDetailTvc *)tvc withDic:(NSDictionary *)dic{

    if (![userInfoTool share].ID) {
        
        LoginViewController *loginVC = [[LoginViewController alloc] init];
        HWNavigationController *loginNaviVC = [[HWNavigationController alloc] initWithRootViewController:loginVC];
        [self presentViewController:loginNaviVC animated:YES completion:nil];
        return;
    }
    [self.navigationController pushViewController:[[confrimOrderVc alloc] initWithDic:dic] animated:YES];
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    NSArray * imageArr = self.detailDic[@"images"];
    return imageArr.count;
}
- (NSInteger)tableView:(UITableView *)tableView sectionForSectionIndexTitle:(NSString *)title atIndex:(NSInteger)index{

    return 1;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    static NSString * ID = @"ProductInfoViewControllerCell";
    ProductInfoViewControllerCell * cell = [tableView dequeueReusableCellWithIdentifier:ID];
    if (cell == nil) {
        cell = [[NSBundle mainBundle] loadNibNamed:@"ProductInfoViewControllerCell" owner:nil options:nil].lastObject;
    }
    NSArray * imageArr = self.detailDic[@"images"];
    cell.infoDic = imageArr[indexPath.row];
    return cell;
}


- (void)scrollViewDidEndScrollingAnimation:(UIScrollView *)scrollView{

}
- (void)scrollViewDidScroll:(UIScrollView *)scrollView{

    if (scrollView.tag == 880) {
       int page = (scrollView.contentOffset.y+[UIScreen mainScreen].bounds.size.height*0.5)/[UIScreen mainScreen].bounds.size.height;
        if (self.pageControl.currentPage!=page) {
            self.pageControl.currentPage = page;
            NSLog(@"cureentPage:%d",page);
            [self.downView setSelectedPage:page];
        }
    }
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{

    return self.tableView.frame.size.height;
}
- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{

    return 0.01;
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{

    return 0.01;
    
    
}
- (void)ProductDownViewDidSelectedWithProductDownView:(ProductDownView *)downView andIndex:(NSInteger)index{

    [self.tableView scrollToRowAtIndexPath:[NSIndexPath indexPathForItem:index inSection:0] atScrollPosition:UITableViewScrollPositionMiddle animated:YES];
}


-(void)didFinishGetUMSocialDataInViewController:(UMSocialResponseEntity *)response {
    
    if (response.responseCode == UMSResponseCodeSuccess) {
        
        NSMutableDictionary *dic = [NSMutableDictionary dictionary];
        dic[@"user_id"] = [userInfoTool share].ID ? [userInfoTool share].ID : @"0";
        dic[@"id"] = self.detailDic[@"id"];
        dic[@"type"] = @"0";
        dic[@"plate"] = @"4";
        [WLHttpRequestAfter usercallBackShareWithParam:dic usingSucessBlock:^(BOOL isSucess, NSDictionary *resultDictionary) {
            
        } andFailureBlock:^(NSError *resultError) {
            
        }];
    }
}

@end
